package com.ruoyi.info.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.TreeSelect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.info.mapper.SysInfoColumnMapper;
import com.ruoyi.info.domain.SysInfoColumn;
import com.ruoyi.info.service.ISysInfoColumnService;

/**
 * 资讯栏目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class SysInfoColumnServiceImpl implements ISysInfoColumnService 
{
    @Autowired
    private SysInfoColumnMapper sysInfoColumnMapper;

    /**
     * 查询资讯栏目
     * 
     * @param columnId 资讯栏目主键
     * @return 资讯栏目
     */
    @Override
    public SysInfoColumn selectSysInfoColumnByColumnId(Long columnId)
    {
        return sysInfoColumnMapper.selectSysInfoColumnByColumnId(columnId);
    }

    /**
     * 查询资讯栏目列表
     * 
     * @param sysInfoColumn 资讯栏目
     * @return 资讯栏目
     */
    @Override
    public List<SysInfoColumn> selectSysInfoColumnList(SysInfoColumn sysInfoColumn)
    {
        return sysInfoColumnMapper.selectSysInfoColumnList(sysInfoColumn);
    }

    /**
     * 新增资讯栏目
     * 
     * @param sysInfoColumn 资讯栏目
     * @return 结果
     */
    @Override
    public int insertSysInfoColumn(SysInfoColumn sysInfoColumn)
    {
        sysInfoColumn.setCreateTime(DateUtils.getNowDate());
        return sysInfoColumnMapper.insertSysInfoColumn(sysInfoColumn);
    }

    /**
     * 修改资讯栏目
     * 
     * @param sysInfoColumn 资讯栏目
     * @return 结果
     */
    @Override
    public int updateSysInfoColumn(SysInfoColumn sysInfoColumn)
    {
        sysInfoColumn.setUpdateTime(DateUtils.getNowDate());
        return sysInfoColumnMapper.updateSysInfoColumn(sysInfoColumn);
    }

    /**
     * 批量删除资讯栏目
     * 
     * @param columnIds 需要删除的资讯栏目主键
     * @return 结果
     */
    @Override
    public int deleteSysInfoColumnByColumnIds(Long[] columnIds)
    {
        return sysInfoColumnMapper.deleteSysInfoColumnByColumnIds(columnIds);
    }

    /**
     * 删除资讯栏目信息
     * 
     * @param columnId 资讯栏目主键
     * @return 结果
     */
    @Override
    public int deleteSysInfoColumnByColumnId(Long columnId)
    {
        return sysInfoColumnMapper.deleteSysInfoColumnByColumnId(columnId);
    }

    /**
     * 校验栏目编码是否唯一
     * 
     * @param sysInfoColumn 栏目信息
     * @return 结果
     */
    @Override
    public String checkColumnCodeUnique(SysInfoColumn sysInfoColumn)
    {
        Long columnId = StringUtils.isNull(sysInfoColumn.getColumnId()) ? -1L : sysInfoColumn.getColumnId();
        SysInfoColumn info = sysInfoColumnMapper.checkColumnCodeUnique(sysInfoColumn.getColumnCode());
        if (StringUtils.isNotNull(info) && info.getColumnId().longValue() != columnId.longValue())
        {
            return "1";
        }
        return "0";
    }

    /**
     * 构建前端所需要树结构
     * 
     * @param columns 栏目列表
     * @return 树结构列表
     */
    @Override
    public List<SysInfoColumn> buildColumnTree(List<SysInfoColumn> columns)
    {
        List<SysInfoColumn> returnList = new ArrayList<SysInfoColumn>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysInfoColumn column : columns)
        {
            tempList.add(column.getColumnId());
        }
        for (SysInfoColumn column : columns)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(column.getParentId()))
            {
                recursionFn(columns, column);
                returnList.add(column);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = columns;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     * 
     * @param columns 栏目列表
     * @return 下拉树结构列表
     */
    @Override
    public List<SysInfoColumn> buildColumnTreeSelect(List<SysInfoColumn> columns)
    {
        List<SysInfoColumn> columnTrees = buildColumnTree(columns);
        return columnTrees;
    }

    /**
     * 递归列表
     * 
     * @param list
     * @param t
     */
    private void recursionFn(List<SysInfoColumn> list, SysInfoColumn t)
    {
        // 得到子节点列表
        List<SysInfoColumn> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysInfoColumn tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysInfoColumn> getChildList(List<SysInfoColumn> list, SysInfoColumn t)
    {
        List<SysInfoColumn> tlist = new ArrayList<SysInfoColumn>();
        for (SysInfoColumn n : list)
        {
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getColumnId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysInfoColumn> list, SysInfoColumn t)
    {
        return getChildList(list, t).size() > 0;
    }
}
